#include <string>
#include <iostream>
#include <stdio.h>
#include <stdarg.h>
#include "log.h"

Log::Log(std::string file)
{
    logFile.open(file.c_str(), std::ios::out);
    std::cout << "Logfile opened: " << file << std::endl;
}

Log::~Log()
{
    close();
}

void Log::close()
{
    logFile.close();
    std::cout << "Logfile closed" << std::endl;
}

void Log::write(const char* txt, ...)
{
    va_list argList;
    char buffer[1024];

    va_start(argList, txt);
    vsnprintf(buffer, 1024, txt, argList);
    va_end(argList);

    std::cout << buffer << std::endl;

    if (logFile.is_open())
    {
        logFile << buffer << std::endl;
    }
}
